---------------------------------------
--         AmBContentSplitter        --
---------------------------------------
-- Can split an area into adjacent areas
-- that all share two edges.
local ContentSplitter = {
   Init = function(self)
		self._Horizontal = true -- Are the areas horizontal
		self._Areas = { }
		self._AreaSplitters = { } -- Location of splitters
   end,
	
	-- Sets the location of the splitters and wether this content
	-- splitter is horizontal. Splitters go by offset from the previous
	-- splitter. The first and last areas are stretched as needed.
	SetSplitters = function(self, IsHorizontal, SplitterLocations)
		self._Horizontal = IsHorizontal
		self._AreaSplitters = SplitterLocations
		local splneed = #(self._AreaSplitters) + 1
		local splhave = #(self._Areas)
		for x = 1, math.max(splneed, splhave) do
			if x > splneed then
				self._Areas[x]:Remove()
				self._Areas[x] = nil
			end
			if x <= splneed and x > splhave then
				local control = vgui.Create("AmBContentPanel", self)
				self._Areas[x] = control
				if self._Horizontal then
					function control.OnChildRequestResize(control, NewWidth, NewHeight)
						return self:OnAreaResize(x, NewWidth, self:GetWide())
					end
				else
					function control.OnChildRequestResize(control, NewWidth, NewHeight)
						return self:OnAreaResize(x, NewHeight, self:GetTall())
					end
				end
			end
		end
		for x = splhave + 1, splneed do
			if self._Areas[x] == nil then
				table.remove(self._Areas)
			end
		end
		self:_PositionAreas()
	end,
	
	-- Sets the location of the splitter at the index
	SetSplitter = function(self, SplitterIndex, SplitterLocation)
		self._AreaSplitters[SplitterIndex] = SplitterLocation
		self:_PositionAreas()
	end,
	
	-- Gets the panel for the specified area.
	GetArea = function(self, Index)
		return self._Areas[Index]
	end,
	
	-- Called when an area needs to be resized. Returns if the resize
	-- can be done.
	OnAreaResize = function(self, Index, NewSize, FullSize)
		return false
	end,
	
   PanelThink = function(self) 
		self:_PositionAreas()
   end,
	
	-- Positions the areas defined for the splitter.
	_PositionAreas = function(self)
		local curpos = 0
		for x = 1, (#self._Areas) do
			local area = self._Areas[x]
			local splitter = self._AreaSplitters[x]
			if x == #self._Areas then
				if self._Horizontal then
					splitter = self:GetWide() - curpos
				else
					splitter = self:GetTall() - curpos
				end
			end
			if self._Horizontal then
				area:SetPos(curpos, 0)
				area:SetSize(splitter, self:GetTall())
			else
				area:SetPos(0, curpos)
				area:SetSize(self:GetWide(), splitter)
			end
			curpos = curpos + splitter
		end
	end
}
AmB.UI.ContentSplitter = ContentSplitter
vgui.Register("AmBContentSplitter", ContentSplitter, "AmBContentPanel")